package cn.uniquetech;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.List;

import cn.uniquetech.util.StrUtil;

public class GenMySqlPojoUtil {

	private static final String PACKAGENAME = "cn.uniquetech";
	private static final String URL = "jdbc:mysql://127.0.0.1:3306/safetysupervision";
	private static final String DBNAME = "safetysupervision";
	private static final String NAME = "root";
	private static final String PASS = "root";
	private static final String DRIVER = "com.mysql.jdbc.Driver";

	private List<String> tableNames;

	public GenMySqlPojoUtil() {
		try {
			tableNames = new ArrayList<String>();
			tableNames.add("thdatas");
			Class.forName(DRIVER);
			Connection con = DriverManager.getConnection(URL, NAME, PASS);
			String sql = "select table_name from information_schema.tables where table_schema='" + DBNAME + "'";
			PreparedStatement ps = null;
			ResultSet rs = null;
			ps = con.prepareStatement(sql);

			ResultSetMetaData metaData = null;
			int colSize = 0;
			for (String tableName : this.tableNames) {
				sql = "select * from " + tableName;
				rs = ps.executeQuery(sql);
				metaData = rs.getMetaData();
				List<String> fields = new ArrayList<String>();
				List<String> fieldTypes = new ArrayList<String>();

				colSize = metaData.getColumnCount();
				for (int i = 0; i < colSize; i++) {
					String field = metaData.getColumnName(i + 1);
					String fieldType = metaData.getColumnTypeName(i + 1);
					fields.add(field);
					fieldTypes.add(fieldType);
				}

				String clazz = StrUtil.initCap(tableName);
				clazz = clazz.substring(0, clazz.length() - 1);
				String str = "";
				str = StrUtil.sc(PACKAGENAME, tableName, fields, fieldTypes, null);
				System.out.println(str);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new GenMySqlPojoUtil();
	}

}
